#include <stdio.h>
#include "utils.h"
int atoi(const char *nptr);
//char HStr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
int Hex2Char(unsigned int i,unsigned char * toc){
	
    if(i>=0 && i<= 9)
       * toc=i + 0x30;
   //    *toc= i + '0';
    if(i>=10 && i<=15)
        * toc=i + 0x37;
        // *toc=  i + 'A' - 10;

//    return HStr[i];
}

int Char2Hex(unsigned char c,unsigned int * toi){
	if(c>='A'&& c<='F')
	*toi=c - 0x37;
	if(c>='0' && c <='9')
		*toi=c - 0x30;
}

int Bitstr2ByteArr(unsigned char * bs, unsigned char * ba){
	int sum=0;
	if (bs[7]=='1') sum=sum+1;
        if (bs[6]=='1') sum=sum+2;
        if (bs[5]=='1') sum=sum+4;
        if (bs[4]=='1') sum=sum+8;
	if (bs[3]=='1') sum=sum+16;
        if (bs[2]=='1') sum=sum+32;
        if (bs[1]=='1') sum=sum+64;
        if (bs[0]=='1') sum=sum+128; 
	int sum2=0;
        if (bs[15]=='1') sum2=sum2+1;
        if (bs[14]=='1') sum2=sum2+2;
        if (bs[13]=='1') sum2=sum2+4;
        if (bs[12]=='1') sum2=sum2+8;
        if (bs[11]=='1') sum2=sum2+16;
        if (bs[10]=='1') sum2=sum2+32;
        if (bs[9]=='1') sum2=sum2+64;
        if (bs[8]=='1') sum2=sum2+128;
        printf("{0x%x,0x%x}\n",sum,sum2);
}
int ByteArr2Bitstr(unsigned char * ba, unsigned char * bs){
	int i,j=0;	
	for(j=0;j<4;j++){
		i=ba[j];
		if(i=='0') printf("0000");
		if(i=='1') printf("0001");       
		if(i=='2') printf("0010");
	        if(i=='3') printf("0011");
		if(i=='4') printf("0100");
	        if(i=='5') printf("0101");
		if(i=='6') printf("0110");
	        if(i=='7') printf("0111");
		if(i=='8') printf("1000");
	        if(i=='9') printf("1001");
		if(i=='a') printf("1010");
	        if(i=='b') printf("1011");
		if(i=='c') printf("1100");
	        if(i=='d') printf("1101");
		if(i=='e') printf("1110");
	        if(i=='f') printf("1111");
	}
	printf("\n");
}
int Int2ByteArr(unsigned int i, unsigned char * ba){
        int d,j=0;
        char res[20]={'0'};
        for(;d=i%16;i/=16) res[j++] =d>9? 'A'+d-10:'0'+d;
	printf("ba = {0x%c%c,0x%c%c,0x%c%c,0x%c%c}",res[7],res[6],res[5],res[4],res[3],res[2],res[1],res[0]);
	printf("\n");
}
int ByteArr2Int(unsigned char * ba,unsigned int * i){
	int res=atoi(ba);
	int sum=0,mul=1;
	int j,r;
	int count=0;
	do{
		r=res%16;
		for(j=0;j<count;j++)
			mul*=16;
		mul*=r;
		sum+=mul;
		mul=1;
		count++; 
	}while(res/=16);
	printf("%d\n",sum);
}





